<?php

/**
 * Add a task
 * 
 * @param string $subject
 * @param int $due
 * @param str $task_type
 * @param int $author_id
 * @param int $user_id
 * @param str $description
 * @param str $status
 * @param int $post_id
 * @return boolean True on success, false on fail
 */
function web_apper_insert_task( $subject, $due, $task_type, $author_id, $user_id, $description, $status, $post_id = null ) {	
	$task                   = new WebApper\Task\Task;
	$task->subject			= $subject;
	$task->due				= $due;
	$task->task_type		= $task_type;
	$task->author_id		= $author_id;
	$task->user_id			= $user_id;
	$task->description		= $description;
	$task->status			= $status;
	$task->post_id			= $post_id;

	if ( $task->save() )
		return true;

	return false;
}

/**
 * Add a task
 * 
 * @param string $subject
 * @param int $due
 * @param str $task_type
 * @param int $author_id
 * @param int $user_id
 * @param str $description
 * @param str $status
 * @param int $post_id
 * @return boolean True on success, false on fail
 */
function web_apper_update_task( $id, $subject, $due, $task_type, $user_id, $description, $status, $post_id = null ) {	
	$task                   = new WebApper\Task\Task($id);
	$task->subject			= $subject;
	$task->due				= $due;
	$task->task_type		= $task_type;
	$task->user_id			= $user_id;
	$task->description		= $description;
	$task->status			= $status;
	$task->post_id			= $post_id;

	if ( $task->save() )
		return true;

	return false;
}

/**
 * Archive a tasks
 *
 * @since WebApper (1.0)
 * @param int $id
 * @return 
 */
function web_apper_archive_task( $id ) {
	$task = new WebApper\Task\Task($id);
	return $task->archive();
}


/**
 * Calculate a taks Due timestamp
 *
 * @param str $due
 * @param str $due_in_unit
 * @param str $due_on_date
 * @param str $due_on_ap_pm
 * @param int $due_on_hour
 * @return int $due The due timestamp
 */
function web_apper_task_due_timestamp( $due, $due_in, $due_in_unit, $due_on_date, $due_on_ap_pm, $due_on_hour  ) {
	if ( $due == 'due_in' ) : // Get a due date for the task
		switch ( $due_in_unit ) :
			case 'hours' : $unit = 60 * 60; break;			
			case 'days' : $unit = 60 * 60 * 24; break;			
			case 'weeks' : $unit = 60 * 60 * 24 * 7; break;			
			case 'months' : $unit = 60 * 60 * 24 * 30; break;			
		endswitch;
		$due = current_time('timestamp') + ( $due_in * $unit );
	elseif ( $due == 'due_on' ) :
		$date = new \DateTime( $due_on_date );
		if ( !empty( $due_on_hour ) ) :
			$due_on_ap_pm == 'am' ? $hours = $due_on_hour : $hours = $due_on_hour + 12;
			$date->add(new \DateInterval('PT' . $hours . 'H'));
		endif;
		$due = $date->getTimestamp();
	endif;
	return $due;
}

/**
 * Auto assign a task to a user
 *
 */
function web_apper_auto_assign_task( $assign_to  ) {
	if ( $assign_to == 'rotate_users' ) :
		$users = get_users( 'orderby=ID&order=ASC&role=staff' );
		$last_rotation = get_option( 'webapper_tasks_user_rotation', null );
		foreach ( $users as $user ) :
			if ( $last_rotation < $user->ID ) :
				$assign_to = $user->ID;
				break;
			endif;
		endforeach;
		if ( is_null($last_rotation) || count($users) == 1 ) :
			$assign_to = $users[0]->ID;
		endif;
		update_option( 'webapper_tasks_user_rotation', $assign_to );
		return $assign_to;
	elseif ( $assign_to == 'least_to_do' ) :	
		// least_to_do - assign task to the user with the least to do on the tasks due date
	endif;
}

/**
 * Get tasks for a specific user
 *
 * @since WebApper (1.0)
 * @param int $user_id
 * @return boolean Object or array on success, false on fail
 */
function web_apper_get_tasks_for_user( $user_id ){
	return $tasks = WebApper\Task\Task::get_all_for_user( $user_id );
}


?>